package Ctr;

import DB.*;
import Model.*;

import java.util.ArrayList;

public class ProductCtr {
    private ConnectionDB connection;
    private ProductDB productDB;

    public ProductCtr(){
        productDB = new ProductDB();
    }

    public Product findProduct(int id) {
        Product po = productDB.findProduct(id);
        return po;
    }

    public Product findProduct(String search, boolean retrieveAssociation){
        return productDB.findProduct(search, false);
    }

    public ArrayList<Product> findProducts (String name, boolean retreiveAssociation) {
        return productDB.findProducts(name, retreiveAssociation);
    }

    public void insertProduct(int id, String name, float purchasePrice, float salesPrice, float rentPrice, String countryOfOrigin, int minStock, String type, int supplier){
        Product productObj = new Product(id, name, purchasePrice, salesPrice, rentPrice, countryOfOrigin, minStock, type, supplier);

        try {
            connection.startTransaction();
            ProductDB productDB = new ProductDB();
            productDB.insertProduct(productObj);
            connection.commitTransaction();
        }
        catch (Exception e){
            connection.rollbackTransaction();
        }
    }

    //Find all products
    public ArrayList<Product> getAllProducts(boolean retrieveAssociation) {
        return productDB.getAllProducts(false);
    }

    //Delete product
    public void deleteProduct(int id) {
        productDB.deleteProduct(id);
    }

    //GetMax
    public int getMaxId() {
        int id = productDB.productGetMax();
        return id;
    }
}
